jquery教程

推荐列表 站点导航

当前位置:首页 > jquery > jquery教程 >

javascript格式化日期时间 自定义格式化函数示例

来源:网络整理  作者:wy  发布时间:2020-12-23 13:45
本文介绍了javascript格式化日期时间的方法,使用自定义格式化函数的例子,有需要的朋友参考下。...

可以利用javascript中date对象的内置方法来格式化,例如:
 

复制代码 代码示例:

var d = new date();
console.log(d); // 输出:mon nov 04 2013 21:50:33 gmt+0800 (中国标准时间)
console.log(d.todatestring()); // 日期字符串,输出:mon nov 04 2013
console.log(d.togmtstring()); // 格林威治时间,输出:mon, 04 nov 2013 14:03:05 gmt
console.log(d.toisostring()); // 国际标准组织(iso)格式,输出:2013-11-04t14:03:05.420z
console.log(d.tojson()); // 输出:2013-11-04t14:03:05.420z
console.log(d.tolocaledatestring()); // 转换为本地日期格式,视环境而定,输出:2013年11月4日
console.log(d.tolocalestring()); // 转换为本地日期和时间格式,视环境而定,输出:2013年11月4日 下午10:03:05
console.log(d.tolocaletimestring()); // 转换为本地时间格式,视环境而定,输出:下午10:03:05
console.log(d.tostring()); // 转换为字符串,输出:mon nov 04 2013 22:03:05 gmt+0800 (中国标准时间)
console.log(d.totimestring()); // 转换为时间字符串,输出:22:03:05 gmt+0800 (中国标准时间)
console.log(d.toutcstring()); // 转换为世界时间,输出:mon, 04 nov 2013 14:03:05 gmt

另外,也可以自定义函数来格式化时间,例如:
 

复制代码 代码示例:

date.prototype.format = function(format) {
  var date = {
    "m+": this.getmonth() + 1,
    "d+": this.getdate(),
    "h+": this.gethours(),
    "m+": this.getminutes(),
    "s+": this.getseconds(),
    "q+": math.floor((this.getmonth() + 3) / 3),
    "s+": this.getmilliseconds()
  };
  if (/(y+)/i.test(format)) {
    format = format.replace(regexp.$1, (this.getfullyear() + '').substr(4 - regexp.$1.length));
  }
  for (var k in date) {
    if (new regexp("(" + k + ")").test(format)) {
      format = format.replace(regexp.$1, regexp.$1.length == 1
        ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
    }
  }
  return format;
}
var d = new date().format('yyyy-mm-dd');
console.log(d); // 2013-11-04

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jq/jc/8196.shtml

相关文章
最新文章
PHP识别相片是否是颠倒的 PHP识别相片是否是颠倒的

时间:2020-12-28

python编程有哪些ide python编程有哪些ide

时间:2020-12-28

python开发工程师是做什么 python开发工程师是做什么

时间:2020-12-28

php构造函数的作用 php构造函数的作用

时间:2020-12-28

php怎么跟数据库连接 php怎么跟数据库连接

时间:2020-12-28

php实现顺序线性表 php实现顺序线性表

时间:2020-12-28

Python多重继承中的菱形继 Python多重继承中的菱形继

时间:2020-12-28

php中break的作用 php中break的作用

时间:2020-12-28

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

javascript格式化日期时间 自定义格式化函数示例

2020-12-23 编辑:wy

可以利用javascript中date对象的内置方法来格式化,例如:
 

复制代码 代码示例:

var d = new date();
console.log(d); // 输出:mon nov 04 2013 21:50:33 gmt+0800 (中国标准时间)
console.log(d.todatestring()); // 日期字符串,输出:mon nov 04 2013
console.log(d.togmtstring()); // 格林威治时间,输出:mon, 04 nov 2013 14:03:05 gmt
console.log(d.toisostring()); // 国际标准组织(iso)格式,输出:2013-11-04t14:03:05.420z
console.log(d.tojson()); // 输出:2013-11-04t14:03:05.420z
console.log(d.tolocaledatestring()); // 转换为本地日期格式,视环境而定,输出:2013年11月4日
console.log(d.tolocalestring()); // 转换为本地日期和时间格式,视环境而定,输出:2013年11月4日 下午10:03:05
console.log(d.tolocaletimestring()); // 转换为本地时间格式,视环境而定,输出:下午10:03:05
console.log(d.tostring()); // 转换为字符串,输出:mon nov 04 2013 22:03:05 gmt+0800 (中国标准时间)
console.log(d.totimestring()); // 转换为时间字符串,输出:22:03:05 gmt+0800 (中国标准时间)
console.log(d.toutcstring()); // 转换为世界时间,输出:mon, 04 nov 2013 14:03:05 gmt

另外,也可以自定义函数来格式化时间,例如:
 

复制代码 代码示例:

date.prototype.format = function(format) {
  var date = {
    "m+": this.getmonth() + 1,
    "d+": this.getdate(),
    "h+": this.gethours(),
    "m+": this.getminutes(),
    "s+": this.getseconds(),
    "q+": math.floor((this.getmonth() + 3) / 3),
    "s+": this.getmilliseconds()
  };
  if (/(y+)/i.test(format)) {
    format = format.replace(regexp.$1, (this.getfullyear() + '').substr(4 - regexp.$1.length));
  }
  for (var k in date) {
    if (new regexp("(" + k + ")").test(format)) {
      format = format.replace(regexp.$1, regexp.$1.length == 1
        ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
    }
  }
  return format;
}
var d = new date().format('yyyy-mm-dd');
console.log(d); // 2013-11-04

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jq/jc/8196.shtml

相关文章

风云图片

推荐阅读

返回jquery教程频道首页